<?php defined('_JEXEC') or die('Restricted access'); 

echo JhtraqHelper::drawTabs($this->project->slug);
$ticket = $this->ticket;
$user =& JFactory::getUser();
$managers = explode("," ,$this->project->managers);
JHTML::_('behavior.formvalidation');

?>

<p></p>
<div class="componentheading">
<?php echo $this->project->name; ?> - <?php echo JText::_('COM_JHTRAQ_VIEW_TICKETS');?> # <?php echo $ticket['ticket_id'];?>
</div>

<div id="ticket">
			<div class="date">
			<p><?php echo JText::_('COM_JHTRAQ_OPENED');?> <?php echo JhtraqHelper::timesince($ticket['created'])?> <?php echo JText::_('COM_JHTRAQ_AGO');?></p>
			<p><?php echo JText::_('COM_JHTRAQ_LAST_MODIFIED');?> <?php ($ticket['updated'] ? JhtraqHelper::timesince($ticket['updated']).JText::_('COM_JHTRAQ_AGO') : JText::_('COM_JHTRAQ_NEVER'))?></p>
			</div>
			<h1 class="summary"><?php echo stripslashes($ticket['summary'])?> <small>(<?php echo JText::_('COM_JHTRAQ_TICKET').$ticket['ticket_id']?>)</small> <?php if(in_array($user->id,$managers)) { ?>
			<input type="button" onclick="if(confirm('<?php echo JText::_('COM_JHTRAQ_CONFIRM_DELETE')?>')) { window.location='<?php echo JRoute::_("index.php?option-com_jhtraq&view=tickets&project=".$this->project->slug . "&task=delete&ticket=".$ticket['id'])?>' }" value="<?php echo JText::_('COM_JHTRAQ_DELETE')?>" />
				<? } ?></h1>
			<table class="properties">
				<tr>
				<th id="h_owner"><?php echo JText::_('COM_JHTRAQ_REPORTED_BY')?>:</th>
					<td headers="h_owner"><?php echo $ticket['user_name']?></td>
					<th id="h_assignee"><?php echo JText::_('COM_JHTRAQ_ASSIGNED_TO')?>:</th>
					<td headers="h_assignee"><?php echo $ticket['assignee']['username']?></td>
				</tr>
				<tr>
				<th id="h_type"><?php echo JText::_('COM_JHTRAQ_TYPE')?>:</th>
					<td headers="h_type"><?php echo $this->ticket_type($ticket['type'])?></td>
					<th id="h_priority"><?php echo JText::_('COM_JHTRAQ_PRIORITY')?>:</th>
					<td headers="h_priority"><?php echo $this->ticket_priority($ticket['priority'])?></td>
				</tr>
				<tr>
				<th id="h_severity"><?php echo JText::_('COM_JHTRAQ_SEVERITY')?>:</th>
					<td headers="h_severity"><?php echo $this->ticket_severity($ticket['severity'])?></td>
					<th id="h_component"><?php echo JText::_('COM_JHTRAQ_COMPONENT')?>:</th>
					<td headers="h_component"><?php echo $ticket['component']['name']?></td>
				</tr>
				<tr>
				<th id="h_milestone"><?php echo JText::_('COM_JHTRAQ_MILESTONE')?>:</th>
					<td headers="h_milestone"><?php echo $ticket['milestone']['milestone']?></td>
					<th id="h_version"><?php echo JText::_('COM_JHTRAQ_VERSION')?>:</th>
					<td headers="h_version"><?php echo $ticket['version']['version']?></td>
				</tr>
				<tr>
				<th id="h_status"><?php echo JText::_('COM_JHTRAQ_STATUS')?>:</th>
					<td headers="h_status"><?php echo $this->ticket_status($ticket['status'])?></td>
				</tr>
				
			</table>
			
			<div class="description">
			<h3 id="description"><?php echo JText::_('COM_JHTRAQ_DESCRIPTION')?></h3>
				<p>
				<?php echo stripslashes(JhtraqHelper::formattext($ticket['body']))?>
				</p>
				<h3><?php echo JText::_('COM_JHTRAQ_ATTACHMENTS')?></h3>
				<p id="attachments">
					<ul>
					<?php foreach($ticket['attachments'] as $attachment) { ?>
						<li>
						
						<strong><a href="<?php echo JRoute::_('index.php?option=com_jhtraq&view=tickets&project='.$this->project->slug.'&task=download&ticket='.$ticket['ticket_id'].'&attachment='.$attachment['id'])?>"><?php echo $attachment['mask']?></a></strong> <?php JText::_('COM_JHTRAQ_ADDED_BY'); ?> <?php echo $attachment['ownername']?> <?php echo JhtraqHelper::timesince($attachment['create_date'])?> <?php echo JText::_('COM_JHTRAQ_AGO');?>.
						</li>
					
					<?php }?>
					</ul>
				</p>
				<?php //if($user->id)
                                if(JhtraqHelper::isAllowed($this->project->add_tickets))
                               { ?>
                                
				<p>
				<form action="<?php echo JRoute::_('index.php?option=com_jhtraq&view=tickets&project='.$this->project->slug.'&ticket='.$ticket['ticket_id'])?>" method="post" enctype="multipart/form-data">
						<input type="hidden" name="task" value="attachfile" />
						<label><?php echo JText::_('COM_JHTRAQ_ATTACH_FILE')?>: <input type="file" name="file" /> <input type="submit" value="<?php echo JText::_('COM_JHTRAQ_ATTACH')?>" /></label>
					</form>
				</p>
				<?php } ?>
			</div>
		</div>
		
		<h3><?php echo JText::_('COM_JHTRAQ_TICKET_HISTORY')?></h3>
		<div id="ticket_history">
			<?php foreach($ticket['changes'] as $change) { 
//    print_r($change);
   ?>
			<div class="ticket_prop_change">
			<h5><?php echo JhtraqHelper::timesince($change['timestamp'],true)?> <?php echo JText::_('COM_JHTRAQ_AGO_BY')?> 
				<?php echo $change['user_name']?>
			</h5>
				<div class="ticket_change_actions">
				<?php if(in_array($user->id,$managers)) { ?>
				<form action="<?php echo JRoute::_('index.php?option=com_jhtraq&view=tickets&project='.$this->project->slug.'&ticket='.$ticket['ticket_id'])?>" method="post">
						<input type="hidden" name="task" value="delete_comment" />
						<input type="hidden" name="comment" value="<?php echo $change['id']?>" />
						<input type="submit" value="<?php echo JText::_('COM_JHTRAQ_DELETE')?>" />
					</form>
					<?php } ?>
				</div>
				<?php 
				$changes = json_decode(stripslashes($change['changes']));
				//print_r($changes);
				if(count($changes) > 0) { ?>
				<ul>
				<?php foreach($changes as $row) { ?>
				<li><?php echo JhtraqHelper::l('ticket_history_'.$row->property.JhtraqHelper::iif($row->action,'_'.$row->action),$row->from,$row->to) //todo?></li>
					<?php } ?>
				</ul>
				<?php } ?>
				<?php if($change['comment'] != '') { ?>
				<div class="change_comment">
				<?php echo stripslashes(JhtraqHelper::formattext($change['comment']))?>
				</div>
				<?php } ?>
			</div>
			<?php } ?>
		</div>
		
		<?php if(in_array($user->id,$managers))  { ?>
		<form action="<?php echo JRoute::_('index.php?option=com_jhtraq&view=tickets&project='.$this->project->slug.'&ticket='.$ticket['id'])?>" method="post">
		<input type="hidden" name="task" value="update" />
		<h3><?php echo JText::_('COM_JHTRAQ_UPDATE_TICKET')?></h3>
		<div id="update_ticket">
			<?php if(count($errors)) { ?>
			<div class="message error">
				<?php foreach($errors as $error) { ?>
				<?php echo $error?><br />
				<?php } ?>
			</div>
			<?php } ?>
			
			<fieldset class="properties">
			<legend><?php echo JText::_('COM_JHTRAQ_TICKET_PROPERTIES')?></legend>
				<table class="properties">
					<tr>
					<th class="col1"><?php echo JText::_('COM_JHTRAQ_TYPE')?></th>
						<td>
							<select name="type">
								<?php $this->buildSelectOptions('type');?>
							</select>
						</td>
						<th class="col2"><?php echo JText::_('COM_JHTRAQ_ASSIGNED_TO')?></thd>
						<td>
							<select name="assign_to">
								<option value="0"></option>
								<?php $this->buildSelectOptions('assigned_to');?>
							</select>
						</td>
					</tr>
					<tr>
					<th class="col1"><?php echo JText::_('COM_JHTRAQ_PRIORITY')?></th>
						<td>
							<select name="priority">
							<?php $this->buildSelectOptions('priority');?>
							</select>
						</td>
						<th class="col2"><?php echo JText::_('COM_JHTRAQ_SEVERITY')?></th>
						<td>
						<select name="severity">
						<?php $this->buildSelectOptions('severity');?>
						</select>
						</td>
					</tr>
					<tr>
					<th class="col1"><?php echo JText::_('COM_JHTRAQ_MILESTONE')?></th>
						<td>
							<select name="milestone">
							<?php $this->buildSelectOptions('milestone');?>
							</select>
						</td>
						<th class="col2"><?php echo JText::_('COM_JHTRAQ_VERSION')?></th>
						<td>
							<select name="version">
							<?php $this->buildSelectOptions('version');?>
							</select>
						</td>
					</tr>
					<tr>
					<th class="col1"><?php echo JText::_('COM_JHTRAQ_COMPONENT')?></th>
						<td>
						<select name="component">
						<?php $this->buildSelectOptions('component');?>
						</select>
						</td>
						<th class="col2"><?php echo JText::_('COM_JHTRAQ_SUMMARY')?></th>
						<td><input type="text" name="summary" value="<?php echo $ticket['summary']?>" /></td>
					</tr>
					<tr>
					<th class="col1"><?php echo JText::_('COM_JHTRAQ_ACTION')?></th>
						<td>
							<?php if(!$ticket['closed']) { ?>
							<input type="radio" name="action" value="mark" checked="checked" id="mark" /> <label for="mark"><?php echo JText::_('COM_JHTRAQ_MARK_AS')?></label> <select name="mark_as">
							<?php $this->buildSelectOptions('status')?>
							</select>
							<br />
							<input type="radio" name="action" value="close" id="close" /> <label for="close"><?php echo JText::_('COM_JHTRAQ_CLOSE_AS')?></label> <select name="close_as">
								<?php $this->buildSelectOptions('status')?>
							</select>
							<? } else if($ticket['closed']) { ?>
							<input type="radio" name="action" value="reopen" id="reopen" /> <label for="reopn"><?php echo JText::_('COM_JHTRAQ_REOPEN_AS')?></label> <select name="reopen_as">
							<?php $this->buildSelectOptions('status')?>
							</select>
							<? } ?>
						</td>
						<th class="col2" valign="top"><label for="private"><?php echo JText::_('COM_JHTRAQ_PRIVATE_TICKET')?></label></th>
						<td><input type="checkbox" name="private" id="private" value="1"<?php if($ticket['private']) echo ' checked="checked"'?> /></td>
					</tr>
	
					<tr>
					<th><?php echo JText::_('COM_JHTRAQ_COMMENTS');?></td><td colspan="2">
					<textarea name="comment" rows="10" cols="50"></textarea></td>
					<td valign="bottom" align="right"><button class="button validate" type="submit"><?php echo JText::_('COM_JHTRAQ_UPDATE'); ?></button></td>
					</tr>
				</table>
			</fieldset>
		</div>
		</form>
		<?php }elseif(JhtraqHelper::isallowed($this->project->comment_tickets)){ ?>
		<form action="<?php echo JRoute::_('index.php?option=com_jhtraq&view=tickets&layout=view&project='.$this->project->slug.'&ticket='.$ticket['ticket_id'])?>" method="post" class="form-validate">
		<input type="hidden" name="task" value="addcomment" />
		<h3><?php echo JText::_('COM_JHTRAQ_ADD_COMMENT')?></h3>
		<table>
			<?php
				$user = JFactory::getUser();
				if(!$user->id) { ?>
				<tr>
					<th><label for="name"><?php echo JText::_('COM_JHTRAQ_YOUR_NAME');?></label></th>
			
				<td><input type="text" class="inputbox required" name="name" id="name" value="<?php echo @$_POST['name'];?>" /></td>
			</tr>
			<?php
			}
				?>
				<tr>
				<th><label for="comment"><?php echo JText::_('COM_JHTRAQ_COMMENTS');?></label></th>
				<td>
					<textarea name="comment" rows="10" id="comments" class="inputbox required" cols="50"><?php echo @$_POST['comment'];?></textarea>
				</td>
				</tr>
				<?php
				if(!$user->id) { ?>
					<tr>
					<th><label for="captcha"><?php echo JText::_('COM_JHTRAQ_CAPTCHA');?></label></th>
					
					<td><?php echo $this->captchaHTML;  ?>
					<br /><?php echo @$this->captcha_error;?></td>
					</tr>
					<?php
					}
					?>
					<tr><td></td>
				<td valign="bottom" align="right">
				<button class="button validate" type="submit"><?php echo JText::_('COM_JHTRAQ_UPDATE'); ?></button>
				</td>
			</tr>
		</table>
		</form>
		<?php
		}?>